package com.sodimac.util.commons
{
	import mx.utils.StringUtil;

	public class UtilString extends StringUtil
	{
	
		/**
	     * Checks if a String is empty ("") or null.
	     *
	     * StringUtil.isEmpty(null)      = true
	     * StringUtil.isEmpty("")        = true
	     * StringUtil.isEmpty(" ")       = false
	     * StringUtil.isEmpty("bob")     = false
	     * StringUtil.isEmpty("  bob  ") = false
	     *
	     * @param str  the String to check, may be null
	     * @return true if the String is empty or null
	     */
	    public static function isEmpty(str:String):Boolean
	    {
	    	if(str==null || str.length==0)
	    	{
	    		return true;
	    	}
	    	else
	    	{
	    		return false;
	    	}
	    }
	    
	    /**
	     * Checks if a String is empty ("") or null.
	     *
	     * StringUtil.isEmpty(null)      = false
	     * StringUtil.isEmpty("")        = false
	     * StringUtil.isEmpty(" ")       = true
	     * StringUtil.isEmpty("bob")     = true
	     * StringUtil.isEmpty("  bob  ") = true
	     *
	     * @param str  the String to check, may be null
	     * @return <code>true</code> if the String is empty or null
	     */
	    public static function isNotEmpty(str:String):Boolean
	    {
	    	return !isEmpty(str);
	    }
	    
	    /**
	     * Checks if a String is whitespace, empty ("") or null.
	     *
	     * StringUtils.isBlank(null)      = true
	     * StringUtils.isBlank("")        = true
	     * StringUtils.isBlank(" ")       = true
	     * StringUtils.isBlank("bob")     = false
	     * StringUtils.isBlank("  bob  ") = false
	     *
	     * @param str  the String to check, may be null
	     * @return <code>true</code> if the String is null, empty or whitespace
	     */
	    public static function isBlank(str:String):Boolean
	    {
	    	return isEmpty(trim(str));
	    }
	    
	    /**
	     * Checks if a String is not whitespace, empty ("") or null.
	     *
	     * StringUtils.isBlank(null)      = false
	     * StringUtils.isBlank("")        = false
	     * StringUtils.isBlank(" ")       = false
	     * StringUtils.isBlank("bob")     = true
	     * StringUtils.isBlank("  bob  ") = true
	     *
	     * @param str  the String to check, may be null
	     * @return <code>true</code> if the String is null, empty or whitespace
	     */
	    public static function isNotBlank(str:String):Boolean
	    {
	    	return !isBlank(str);
	    }
	    
	    /**
		 * String returning <code>null</code> if the String is
	     * empty ("") after the trim or if it is <code>null</code>.
	     *
	     * <pre>
	     * StringUtils.trimToNull(null)          = null
	     * StringUtils.trimToNull("")            = null
	     * StringUtils.trimToNull("     ")       = null
	     * StringUtils.trimToNull("abc")         = "abc"
	     * StringUtils.trimToNull("    abc    ") = "abc"
	     * </pre>
	     *
	     * @param str  the String to be trimmed, may be null
	     * @return the trimmed String,
	     *  <code>null</code> if only chars &lt;= 32, empty or null String input
	     */
	    public static function trimToNull(str:String):String 
	    {
	        str = trim(str);
	        return isEmpty(str) ? null : str;
	    }
	
		/**
		 * Compares two string ignoring case to see if they are equal
		 * 
		 * @param str1 First string to be compared
		 * @param str2 Second string to be compared
		 * @return true if the strings are equal
		 */
		public static function equalsIgnoreCase(str1:String, str2:String):Boolean
		{
			return (toLowerCase(str1) == toLowerCase(str2));
		}
		
		/**
		 * Takes a string and coverts it to upper case handling null.
		 * 
		 * @param str to upcase
		 * @return the upcased string
		 */
		public static function toUpperCase(str:String):String
		{
			return str==null?null:str.toUpperCase();
		}
		
		/**
		 * Takes a string and coverts it to lower case handling null.
		 * 
		 * @param str to lower
		 * @return the lower case string
		 */
		public static function toLowerCase(str:String):String
		{
			return str==null?null:str.toLocaleLowerCase();
		}
		
		/**
	     * <p>Returns either the passed in String, or if the String is
	     * empty or <code>null</code>, the value of <code>defaultStr</code>.</p>
	     *
	     * <pre>
	     * StringUtils.defaultIfEmpty(null, "NULL")  = "NULL"
	     * StringUtils.defaultIfEmpty("", "NULL")    = "NULL"
	     * StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
	     * </pre>
	     *
	     * @param str  the String to check, may be null
	     * @param defaultStr  the default String to return
	     *  if the input is empty ("") or <code>null</code>, may be null
	     * @return the passed in String, or the default
	     */
	    public static function defaultIfEmpty(str:String, defaultStr:String):String
	    {
	        return isEmpty(str) ? defaultStr : str;
	    }
	    
	    
	    public static function trimString(str:String):String 
	    {
	        str = trim(str);
	        return isEmpty(str) ? "" : str;
	    }	    
	    
	}    
}